<template>
  <div class="container">
    <button
      class="edit-button"
      @click="openDrawer('drawer2')"
    >
      <span class="edit-button-span">📝</span>
    </button>
    <el-drawer
      title="留言板"
      :visible.sync="drawerVisible.drawer2"
      direction="rtl"
      size="350px"
      :modal="false"
      :show-close="false"
      custom-class="draw-comments"
      @close="closeDrawer('drawer2')"
    >
      <div class="buy-img">
        <div class="img-word"><span>上传图片</span></div>
        <!-- 图像预览 -->
        <div class="pre_img">
          <!-- 图片，用来展示用户选择的头像 -->
          <img
            v-if="!pre_img"
            class="the_img"
            src="../../../../assets/defautl.svg"
            alt=""
          />
          <!-- 这里preavatar是没有压缩的 -->
          <img
            class="the_img"
            alt=""
            v-else
            :src="pre_img"
          >
        </div>

        <!-- 按钮区域 -->
        <div class="btn-box">
          <input
            type="file"
            ref="iptRef"
            accept="image/*"
            style="display: none"
            @change="onFileChange"
          />
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="chooseImg"
          >选择图片</el-button>

        </div>
      </div>
      <div class="buy-dir">
        <div class="img-word"><span>留言</span></div>
        <div class="userWords">
          <textarea
            class="message"
            placeholder="请输入..."
            v-model="formdata.message"
          ></textarea>
        </div>
      </div>

      <el-button
        type="success"
        icon="el-icon-upload"
        @click="addHelpGoods"
      >提交</el-button>
    </el-drawer>
    <HelpCard
      v-for="(item,index) in helpCards"
      :key="index"
      :card='item'
    ></HelpCard>

  </div>
</template>

<script>
import HelpCard from "./helpCards";
import { mapState } from "vuex";
import { addHelpGoodsAPI } from "@/api";
export default {
  data() {
    return {
      isMask: false,
      pre_img: "",

      helpImg: false,
      drawerVisible: {
        drawer1: false,
        drawer2: false,
      },
      filename: "",
      file: "",
      formdata: {
        message: "",
        img_url: "",
      },
    };
  },
  methods: {
    hadMaster() {
      this.isMask = true;
    },
    rebackMask() {
      this.isMask = false;
    },
    // 开关抽屉
    openDrawer(drawerName) {
      console.log(drawerName);
      if (drawerName == "drawer1") {
        this.drawerVisible.drawer1 = true;
      } else {
        this.drawerVisible.drawer2 = true;
      }
    },
    closeDrawer(drawername) {
      if (drawername == "drawer1") {
        this.drawerVisible.drawer1 = false;
      } else {
        this.drawerVisible.drawer2 = false;
      }
    },
    // 选择图片
    chooseImg() {
      this.$refs.iptRef.click();
    },
    onFileChange(e) {
      let files = e.target.files;
      if (files.length == 0) {
        // 证明没有选
      } else {
        this.file = e.target.files[0];
        this.filename = this.file.name;
        const reader = new FileReader();
        reader.readAsDataURL(this.file);
        reader.onload = (e) => {
          this.compressImage2(e.target.result); //处理图片  `
          this.pre_img = e.target.result; //未压缩
        };
      }
    },
    // 压缩图片
    compressImage2: function (base64data) {
      var _this = this;
      var maxWidth = 800; // 设置最大宽度
      var maxHeight = 600; // 设置最大高度
      var quality = 0.7; // 设置压缩质量

      var img = new Image();

      img.onload = function () {
        var width = img.width;
        var height = img.height;

        // 计算压缩后的尺寸
        if (width > height) {
          if (width > maxWidth) {
            height *= maxWidth / width;
            width = maxWidth;
          }
        } else {
          if (height > maxHeight) {
            width *= maxHeight / height;
            height = maxHeight;
          }
        }

        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        canvas.width = width;
        canvas.height = height;
        ctx.drawImage(img, 0, 0, width, height);

        // 将 Canvas 数据转换为 Base64 字符串
        var compressedData = canvas.toDataURL("image/jpeg", quality);

        _this.formdata.img_url = compressedData;
      };

      img.src = base64data;
    },

    // 提交发布求助卡片
    async addHelpGoods() {
      // 获取用户信息
      await this.$store.dispatch("initUserInfo");
      const res = await addHelpGoodsAPI(this.formdata);
      if (res.status == 0) this.$message.success("发布成功");

      console.log("测试测试测试", this.helpCards);
      this.$store.dispatch("initHelpGoods");
      this.formdata.message = "";
      this.formdata.img_url = "";
      setTimeout(() => {
        this.closeDrawer("drawer2");
      }, 500);
    },
  },
  computed: {
    ...mapState({
      helpCards: (state) => state.goods.helpCards,
    }),
  },
  components: {
    HelpCard,
  },
  mounted() {
    console.log("233", this.helpCards);
  },
};
</script>

<style lang='less' scoped>
// 用户发表找寻物体

/* 提交图片滚动条样式 */
/*定义滚动条宽度和颜色*/
.pre_img::-webkit-scrollbar {
  width: 4px;
}

.pre_img::-webkit-scrollbar-track {
  background: #d2f3ef;
}

.pre_img::-webkit-scrollbar-thumb {
  background: #94f0f0;
}

/* 悬停效果 */
.pre_img::-webkit-scrollbar-thumb:hover {
  background: #5cd0f4;
}
/*提交按钮 */
.el-button--success {
  margin-bottom: 20px;
}

/* 商品上传用户头像 */

/* 商品上传头像信息 */

.img-word {
  width: 100%;
  display: flex;
  justify-content: start;
  align-items: center;
  margin-bottom: 10px;
  opacity: 0.8;
}

.buy-img {
  display: flex;
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.pre_img {
  width: 250px;
  height: 250px;
  overflow-y: auto;
  border: 1px solid;
  margin-bottom: 15px;
}
.pre_img > img {
  width: 100%;
  height: auto;
}
/* 结束 */
/* 物品说明介绍 */
.buy-dir {
  width: 100%;
  height: 40%;
  display: flex;

  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  margin-top: -10px;
  opacity: 0.6;
}
.userWords {
  display: flex;
  background-color: #acdccec3;
  margin-top: 4px;
  width: 100%;
  height: 40%;
  padding: 10px;
  justify-content: center;
  align-items: center;
  position: relative;
  box-sizing: border-box;
  max-width: 300px;
  font-family: cursive;
  font-size: 20px;
  border-radius: 10px;
  background: #fff;
  background-image: linear-gradient(#f5f5f0 1.1rem, #ccc 1.2rem);
  background-size: 100% 1.2rem;
  line-height: 1.45rem;
  padding: 0.5rem 0.2rem 0.1rem 0.1rem;
}
/* textarea */
.message {
  background: none;
  /* border: none; */
  border: none;
  resize: none;
  outline: none;
  box-sizing: border-box;
  padding: 8px;
  width: 100%;
  height: 100%;
  margin-top: 12px;
}

.userWords > span {
  width: 96%;
  height: 90%;
  overflow: auto; /* 溢出隐藏 */
  text-align: center;
}
/*定义滚动条宽度和颜色*/
.userWords-span::-webkit-scrollbar {
  width: 4px;
}

.userWords-span::-webkit-scrollbar-track {
  background: #d2f3ef;
}

.userWords-span::-webkit-scrollbar-thumb {
  background: #94f0f0;
}

/* 悬停效果 */
.userWords-span::-webkit-scrollbar-thumb:hover {
  background: #5cd0f4;
}

/* 结束 */

.container {
  display: flex;
  flex-wrap: wrap; /* 当超出容器宽度时自动换行 */
  height: 100%;
  justify-content: center;
  align-items: center;
}
.icon {
  width: 90%;
  height: 90%;
}
.reback-btn {
  width: 33px;
  height: 33px;
  opacity: 0.8;
  cursor: pointer;
}
// mask 蒙层
.mask {
  display: flex;
  position: absolute;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 65%;
  opacity: 0.7;
  top: 0;
  left: 0;
  background-color: #c3c3c3;
  border-radius: 10px;
}
.mask.mask-active {
  display: flex;
}

/* 编辑按钮 */
.edit-button {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #e8e8e8;
  background-color: #959ff8;
  width: 60px;
  height: 60px;
  font-size: 24px;
  text-transform: uppercase;
  border: 2px solid #5bd6fcbb;
  border-radius: 50%;
  transition: all 0.6s cubic-bezier(0.23, 1, 0.32, 1);
  box-shadow: 5px 5px 2px rgba(0, 0, 0, 0.15), 2px 2px 2px rgba(0, 0, 0, 0.1),
    -3px -3px 2px rgba(255, 255, 255, 0.05),
    -2px -2px 1px rgba(255, 255, 255, 0.05);
  overflow: hidden;
  cursor: pointer;
  z-index: 7;
  right: 3%;
  top: 13%;
}
.edit-button-span {
  position: relative;
  z-index: 2;
  transition: all 0.6s cubic-bezier(0.23, 1, 0.32, 1);
}
/* 编辑按钮结束 */

//  卡片底部留言

/* 结束 */

.container {
  display: flex;
  position: relative;
  flex-wrap: wrap; /* 当超出容器宽度时自动换行 */

  height: 100%;
  justify-content: center;
  align-items: center;
}
.card {
  padding: 20px;
  display: flex;
  width: 330px;
  position: relative;
  flex-direction: column;
  justify-content: space-between;
  height: 480px;
  border-radius: 20px;
  background: #e8e8e8;
  box-shadow: 2px 2px 4px #dadada, -2px -2px 4px #f6f6f6;
  transition: 0.4s;
  margin-left: 26px;
  margin-bottom: 25px;
}
.footer {
  display: flex;
  justify-content: right;
  align-items: center;
}

.card:hover {
  translate: 0 -10px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #2e54a7;
  margin: 15px 0 0 10px;
}

.card-image {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 170px;
  overflow: hidden;
  background-color: #c9c9c9;
  border-radius: 15px;
  box-shadow: inset 8px 8px 10px #c3c3c3, inset -8px -8px 10px #cfcfcf;
}
.card-image > img {
  width: 100%;
  height: 100%;
}
.card-body {
  margin: 13px 0 0 10px;
  color: rgb(31, 31, 31);
  font-size: 15px;
  min-height: 80px;
}

.footer {
  float: right;
  margin: 28px 0 0 18px;
  font-size: 13px;
  color: #636363;
}

.by-name {
  font-weight: 700;
}

// 底部卡片评论留言区
/*卡牌图片底部评论 */
/* 底部留言 */
.card-footer {
  display: flex;

  justify-content: space-around;
  align-items: center;
  margin-top: 30px;
}

.icon {
  width: 25px;
  height: 25px;
}
.cardUserInfo {
  display: flex;
  justify-content: center;
  align-items: center;
}
.uesrInfo-img {
  width: 60px;
  height: 60px;
  overflow: hidden;
  border: 1px solid #f3f690;
  border-radius: 100%;
  margin-right: 25px;
}
.uesrInfo-img > span {
  padding: 2px;
}
.comLike {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 20px;
  margin-right: 5px;
}
.comment {
  display: flex;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;
}

.foot-com {
  display: flex;
  padding-left: 20px;
}
.foot-com > span {
  padding-left: 6px;
  opacity: 0.7;
}
.comment > span {
  margin-bottom: -30px;
  padding-top: 6px;
  padding-left: 25px;
  opacity: 0.5;
}
/* 抽屉用户评论 */
.user-comments {
  display: flex;
  flex-direction: column;
  max-height: 80%;
  overflow: auto;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  height: 370px;
  border: 2px solid #959ff8;
  background-color: #7dbbe7a7;
}

/*定义滚动条宽度和颜色*/
.user-comments::-webkit-scrollbar {
  width: 4px;
}

.user-comments::-webkit-scrollbar-track {
  background: #d2f3ef;
}

.user-comments::-webkit-scrollbar-thumb {
  background: #94f0f0;
}

/* 悬停效果 */
.user-comments::-webkit-scrollbar-thumb:hover {
  background: #5cd0f4;
}
.each-comments {
  width: 100%;
  display: flex;

  justify-content: flex-start;
  align-items: flex-end;
  margin-top: 20px;
}
.comment-words {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.comment-words > P {
  padding: 3px;
  padding-left: 6px;
  font-size: 0.8rem;
  opacity: 0.5;
}
.comment-words > span {
  font-size: 0.9rem;
  padding-top: 7px;
  min-height: 20px;
  min-width: 70%;
  margin-left: 7px;
  margin-right: 10px;
  padding: 3px;
  background-color: #e8e8e8;
}

.comInfo {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 30%;
}
.comInfo > p {
  opacity: 0.6;
  font-size: 0.8rem;
}
.comInfo > div {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  border: 3px solid #959ff8;
  overflow: hidden;
}

.comInfo > div > img {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

/* 抽屉评论 */
::v-deep .el-drawer__body {
  width: 95%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
::v-deep .el-drawer.rtl {
  height: 100%;
  background-color: #b6dbfe;
  border-radius: 10px;
  right: 2%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
::v-deep .el-drawer__container {
  position: relative;

  display: flex;

  left: 0;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  bottom: 0;
  height: 90%;
  width: 100%;
}

::v-deep .el-drawer__wrap.draw-comments {
  background-color: #98c5e5;
}
/* 留言板块 */
.feedback-form {
  background: #a9cfff;
  border: 1px solid #8597b1;
  height: 34%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin-bottom: 20px;
  border-radius: 10px;
  padding: 10px;
  font-size: 14px;
}

.form-title {
  text-align: center;
  color: #9ca3af;
  font-size: 20px;
  font-weight: bold;
}

.feedback-input {
  background: #a0bbe1;
  height: 100px;
  border: 1px solid #a9c9ff;
  margin-top: 3px;
  color: #4b5563;
  width: 95%;
  resize: none;
  outline: none;
  border-radius: 5px;
  padding: 10px;
  transition: border-color 0.3s;
}

.submit-button {
  fill: #d1d5db;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  padding: 10px;
  transition: all 0.3s;
  border: 1px solid #4b5563;
  background: #4b5563;
  cursor: pointer;
}

.submit-button:hover {
  fill: #d1d5db;
  border: 1px solid #4b5563;
  background: #2563eb;
}

.secondary-button {
  stroke: #d1d5db;
  background: #4b5563;
  border: 1px solid #4b5563;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
  padding: 3px;
  padding-top: 10px;
  transition: all 0.3s;
  cursor: pointer;
}

.secondary-button:hover {
  stroke: #2563eb;
  border: 1px solid #4b5563;
  background: #2563eb;
}

.spacer {
  margin-top: 20px;
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: flex-end;
}
/* 留言结束 */
</style>